package com.web.hilover.controller;

import com.web.hilover.entity.User;
import com.web.hilover.entity.Couple;
import com.web.hilover.entity.Comment;
import com.web.hilover.entity.Timeline;
import com.web.hilover.service.CoupleService;
import com.web.hilover.service.TimelineService;
import com.web.hilover.service.CommentService;
import com.web.hilover.service.LikeService;
import com.web.hilover.mapper.CoupleMapper;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/couples")
@Slf4j
public class CoupleController {
    
    @Autowired
    private CoupleService coupleService;
    
    @Autowired
    private TimelineService timelineService;
    
    @Autowired
    private CommentService commentService;
    
    @Autowired
    private LikeService likeService;
    
    @Autowired
    private CoupleMapper coupleMapper;
    
    @GetMapping("")
    public String list(@RequestParam(required = false) Integer year, Model model) {
        log.info("Accessing couples list page, year: {}", year);
        
        // 获取所有年份
        List<Integer> years = coupleMapper.findAllYears();
        model.addAttribute("years", years);
        
        // 获取情侣列表
        List<Couple> couples;
        if (year != null) {
            couples = coupleService.getCouplesByYear(year);
        } else {
            couples = coupleService.getAllPublicCouples();
        }
        
        log.info("Found {} couples", couples.size());
        model.addAttribute("couples", couples);
        model.addAttribute("selectedYear", year);
        return "couple/list";
    }
    
    @GetMapping("/{id}")
    public String coupleDetail(@PathVariable Long id, Model model) {
        Couple couple = coupleService.getCoupleById(id);
        if (couple == null) {
            return "redirect:/couples";
        }
        
        List<Timeline> timelines = timelineService.getTimelinesByCouple(id);
        List<Comment> comments = commentService.getCommentsByCouple(id);
        
        model.addAttribute("couple", couple);
        model.addAttribute("timelines", timelines);
        model.addAttribute("comments", comments);
        return "couple/detail";
    }
    
    @PostMapping("/{id}/like")
    @ResponseBody
    public Map<String, Object> likeCouple(@PathVariable Long id, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            return Map.of("success", false, "message", "请先登录");
        }
        
        try {
            likeService.likeCouple(id, user.getId());
            return Map.of("success", true);
        } catch (Exception e) {
            return Map.of("success", false, "message", e.getMessage());
        }
    }
    
    @PostMapping("/{id}/comment")
    @ResponseBody
    public Map<String, Object> addComment(@PathVariable Long id, 
                                        @RequestParam String content,
                                        HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            return Map.of("success", false, "message", "请先登录");
        }
        
        try {
            Comment comment = Comment.builder()
                    .coupleId(id)
                    .userId(user.getId())
                    .content(content)
                    .build();
            commentService.addComment(comment);
            return Map.of("success", true);
        } catch (Exception e) {
            return Map.of("success", false, "message", e.getMessage());
        }
    }
    
    @GetMapping("/search")
    public String search(@RequestParam String keyword, Model model) {
        List<Couple> couples = coupleService.searchCouples(keyword);
        model.addAttribute("couples", couples);
        model.addAttribute("keyword", keyword);
        return "couple/search";
    }
} 